import { Language,EnableList } from '../types';
import apiClient from './client';

interface ApiResponseItem {
  lang_id: string; // 对应 Language 的 id
  name: string;
  dsc: string;    // 对应 Language 的 description
  enable: boolean;
}



// 获取语言列表
export const getLanguages = async (page: number = 1, pageSize: number = 10): Promise<Language[]> => {
  const response = await apiClient.get<{ data: ApiResponseItem[] }>('/lan/v1/languages/', { params: { page, pageSize } });
  // 映射字段并确保类型匹配
  return response.data.data.map((item: ApiResponseItem): Language => ({
    id: item.lang_id,          // 对应 Language 的 id
    name: item.name,
    description: item.dsc,     // 对应 Language 的 description
    enable: item.enable,
  }));
};

//获取启用的语种
export  const  getEnableList= async ():Promise<EnableList[]> =>{
  const response = await apiClient.get(`/lan/v1/languages/enabled`);
  return response.data.data.map((item: ApiResponseItem): EnableList => ({
    id: item.lang_id,          // 对应 Language 的 id
    name: item.name,
  }));
}

// 获取单个语言
export const getLanguage = async (id: string): Promise<Language> => {
  const response = await apiClient.get(`/languages/${id}`);
  return response.data;
};

// 创建语言
export const createLanguage = async (languageData: {
  lang_id: string;
  name: string;
  dsc: string;
  enabled: boolean;
}): Promise<Language> => {
  const response = await apiClient.post('/lan/v1/languages/', languageData);
  return {
    id: response.data.lang_id,
    name: response.data.name,
    description: response.data.dsc,
    enable: response.data.enabled,
  };
};

// 更新语言
export const updateLanguage = async (id: string, languageData: { name: string; dsc: string; enabled: boolean }): Promise<Language> => {
  const response = await apiClient.put(`/lan/v1/languages/${id}`, languageData);
  return response.data;
};

// 更新语言启用状态
export const updateLanguageEnable = async (id: string, enabled: boolean): Promise<void> => {
  await apiClient.put(`/lan/v1/languages/${id}/enable`, { enabled });
};

// 删除语言
export const deleteLanguage = async (id: string): Promise<void> => {
  await apiClient.delete(`/lan/v1/languages/${id}`);
};